package com.cheng.rebuildproject.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cheng.rebuildproject.mapper.UserMapper;
import com.cheng.rebuildproject.pojo.LoginUser;
import com.cheng.rebuildproject.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询用户信息
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //封装条件
        lambdaQueryWrapper.eq(User::getUserName, username);
        //sql语句
        User user = userMapper.selectOne(lambdaQueryWrapper);

        if(Objects.isNull(user)){
            throw new RuntimeException("用户名不存在");
        }



        //权限操作
        List<String> getUserRoleList = userMapper.getUserRoleById(String.valueOf(user.getId()));

        return new LoginUser(user,getUserRoleList);
    }
}
